<template>
    <div class="common-page page-corp-companys-helper" v-loading.fullscreen="loading">
        <div class="common-page__head">
            <!-- <comp-paths class="route" :paths="['/corp', $route.fullPath]"></comp-paths> -->
            <div class="title">
                <span class="text">{{ $options.title }}</span>
                <el-button type="plain" size="small" icon="el-icon-plus" style="margin: 0 12px 0 0" @click="addCompany()" v-if="self">添加企业/项目</el-button>
                <el-dropdown trigger="click" style="margin: 0 12px 0 0" v-if="self">
                    <el-button type="plain" size="small" icon="el-icon-download">导出企业/项目</el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="exportCompanys()">导出企业/项目信息</el-dropdown-item>
                        <el-dropdown-item @click.native="exportCompanysAttendances()">导出企业/项目走访记录</el-dropdown-item>
                        <el-dropdown-item @click.native="exportCompanysUsed()">导出企业/项目使用情况</el-dropdown-item>
                        <el-dropdown-item @click.native="exportCompanysOrders()">导出企业/项目事项信息</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
        <comp-companys class="common-page__body" ref="list" limit="helper" select link>
            <!-- <template #head="scope">
                <el-button type="plain" size="small" icon="el-icon-refresh" @click="search()">刷新列表</el-button>
            </template> -->
            <template #body="scope">
                <el-button type="primary" size="mini" @click.stop="getCompany(scope.data)">详情</el-button>
            </template>
        </comp-companys>
    </div>
</template>

<script>
    import mixin from "../mixin";
    import { string, int, deepCopy } from "../util";
    export default {
        name: "page-corp-companys-helper",
        title: "分管的企业/项目",
        nodes: ["/menu/corp"],
        childs: [],
        mixins: [mixin],
        data: () => ({
            loading: false,
            self: false,
        }),
        methods: {
            search() {
                if (this.loading) return;
                this.$nextTick(() => this.$refs.list.init());
            },
            addCompany() {
                if (this.loading) return;
                this.$router.push("/corp/company/add");
            },
            getCompany(company) {
                if (this.loading) return;
                this.$router.push("/corp/company?companyId=" + company.id);
            },
            exportCompanys() {
                if (this.loading) return;
                let filters = deepCopy(this.$refs.list.filters);
                let content = "当前筛选条件下的 <span style='color:red;'>全部共" + filters.total + "个企业/项目的信息</span> ";
                if (filters.ids && filters.ids.length > 0) content = " <span style='color:red;'>已勾选的" + filters.ids.length + "个企业/项目的信息</span> ";
                let callback = () => {
                    this.loading = true;
                    this.plugins.user.exportCompanys(filters).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.alert("导出任务创建成功！", "任务执行需要花费一些时间，可稍后再点击右上角打开“系统任务”查看任务状态。", "success", ["查看任务状态", "稍后查看"]).then((ok) => ok && this.$router.push("/user/tasks"));
                        } else {
                            this.plugins.modal.alert("导出失败", res.code + "：" + res.msg, "error");
                        }
                    });
                };
                this.plugins.modal.alert("批量导出", "确定导出" + content + "吗？", "warning", ["确定导出", "再想想"]).then((ok) => ok && callback());
            },
            exportCompanysAttendances() {
                if (this.loading) return;
                let filters = deepCopy(this.$refs.list.filters);
                let content = "当前筛选条件下的 <span style='color:red;'>全部共" + filters.total + "个企业/项目的走访记录</span> ";
                if (filters.ids && filters.ids.length > 0) content = " <span style='color:red;'>已勾选的" + filters.ids.length + "个企业/项目的走访记录</span> ";
                let callback = () => {
                    this.loading = true;
                    this.plugins.user.exportCompanysAttendances(filters).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.alert("导出任务创建成功！", "任务执行需要花费一些时间，可稍后再点击右上角打开“系统任务”查看任务状态。", "success", ["查看任务状态", "稍后查看"]).then((ok) => ok && this.$router.push("/user/tasks"));
                        } else {
                            this.plugins.modal.alert("导出失败", res.code + "：" + res.msg, "error");
                        }
                    });
                };
                this.plugins.modal.alert("批量导出", "确定导出" + content + "吗？", "warning", ["确定导出", "再想想"]).then((ok) => ok && callback());
            },
            exportCompanysUsed() {
                if (this.loading) return;
                let filters = deepCopy(this.$refs.list.filters);
                let content = "当前筛选条件下的 <span style='color:red;'>全部共" + filters.total + "个企业/项目的使用情况</span> ";
                if (filters.ids && filters.ids.length > 0) content = " <span style='color:red;'>已勾选的" + filters.ids.length + "个企业/项目的使用情况</span> ";
                let callback = () => {
                    this.loading = true;
                    this.plugins.user.exportCompanysUsed(filters).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.alert("导出任务创建成功！", "任务执行需要花费一些时间，可稍后再点击右上角打开“系统任务”查看任务状态。", "success", ["查看任务状态", "稍后查看"]).then((ok) => ok && this.$router.push("/user/tasks"));
                        } else {
                            this.plugins.modal.alert("导出失败", res.code + "：" + res.msg, "error");
                        }
                    });
                };
                this.plugins.modal.alert("批量导出", "确定导出" + content + "吗？", "warning", ["确定导出", "再想想"]).then((ok) => ok && callback());
            },
            exportCompanysOrders() {
                if (this.loading) return;
                let filters = deepCopy(this.$refs.list.filters);
                let content = "当前筛选条件下的 <span style='color:red;'>全部共" + filters.total + "个企业/项目的事项信息</span> ";
                if (filters.ids && filters.ids.length > 0) content = " <span style='color:red;'>已勾选的" + filters.ids.length + "个企业/项目的事项信息</span> ";
                let callback = () => {
                    this.loading = true;
                    this.plugins.user.exportCompanysOrders(filters).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.alert("导出任务创建成功！", "任务执行需要花费一些时间，可稍后再点击右上角打开“系统任务”查看任务状态。", "success", ["查看任务状态", "稍后查看"]).then((ok) => ok && this.$router.push("/user/tasks"));
                        } else {
                            this.plugins.modal.alert("导出失败", res.code + "：" + res.msg, "error");
                        }
                    });
                };
                this.plugins.modal.alert("批量导出", "确定导出" + content + "吗？", "warning", ["确定导出", "再想想"]).then((ok) => ok && callback());
            },
            destroy() {
                if (this.loading) return;
                return true;
            },
            init() {
                if (this.loading) return;
                this.loading = true;
                this.$root.$refs.navbar.init();
                this.plugins.user.call(async () => {
                    let res = await this.plugins.user.init();
                    if (res.code === 0) { }
                    return res;
                }, async () => {
                    if (this.access(this.$route.name)) return { code: 0 };
                    return this.env.xhrErrors.find((error) => error.code === 4);
                }).then((res) => {
                    this.loading = false;
                    this.$root.$refs.navbar.init();
                    if (res.code === 0) {
                        this.self = this.profile.role === "holder";
                        this.search();
                    } else if (res.code === 2) {
                        this.$root.$refs.navbar.login();
                    } else {
                        this.plugins.modal.alert("页面加载失败", res.code + "：" + res.msg, "error").then(() => this.$router.replace("/home"));
                    }
                });
            },
        },
    };
</script>

<style lang="scss">
    .page-corp-companys-helper {
        overflow: hidden;
    }
</style>